package com.flower.controller;

import com.flower.pojo.Flower;
import com.flower.pojo.Shopping;
import com.flower.pojo.User;
import com.flower.service.ShoppingService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shopping")
public class ShoppingController {
    @Autowired
    private ShoppingService shoppingService;

    @RequestMapping("/queryShopping")
    public Map<String, Object> queryShopping(HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        Integer uid = 1;
        try {
            uid = ((User) session.getAttribute("user")).getUid();
        } catch (Exception e) {

        }
        List<Map<String, Object>> list = shoppingService.queryShopping(uid);
        if (list.size() > 0) {
            map.put("code", 200);
            map.put("msg", "查询成功");
            map.put("data", list);
        } else {
            map.put("code", 201);
            map.put("msg", "暂无数据类型");
        }
        return map;
    }

    @RequestMapping("/delShopping/{spid}")
    public Map<String, Object> delShopping(@PathVariable("spid") Integer spid) {
        Map<String, Object> map = new HashMap<>();
        int result = shoppingService.delShopping(spid);
        if (result != -1) {
            map.put("code", 200);
            map.put("msg", "删除成功");
        } else {
            map.put("code", 201);
            map.put("msg", "删除失败");
        }
        return map;
    }

    @RequestMapping("updateShopping/{spid}/{fnum}/{total}")
    public Map<String, Object> updateShopping(@PathVariable("spid") Integer spid, @PathVariable("fnum") Integer fnum, @PathVariable("total") Double total) {
        Map<String, Object> map = new HashMap<>();
        Shopping shopping = new Shopping();
        shopping.setSpid(spid);
        shopping.setFnum(fnum);
        shopping.setTotal(total);
        int result = shoppingService.uptateShopping(shopping);
        if (result != -1) {
            map.put("code", 200);
            map.put("msg", "修改数量成功");
        } else {
            map.put("code", 201);
            map.put("msg", "修改数量失败");
        }
        return map;
    }

    @RequestMapping("/addShopping")
    public Map<String, Object> addShopping(@RequestBody Map<String, Object> map1, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        System.out.println(map1.get("fid"));
        Flower flower = shoppingService.queryFlowerById(Integer.parseInt(map1.get("fid").toString()));
        Integer fnum = Integer.parseInt(map1.get("fnum").toString());
        Double total = (flower.getPrice()) * fnum;
        Shopping shopping = shoppingService.queryShoppingByFid(flower.getFid());
        int uid = 1;
        try {
            uid = ((User) session.getAttribute("user")).getUid();
        } catch (Exception e) {
        }
        if (shopping == null) {
            Shopping shopping1 = new Shopping();
            shopping1.setTotal(total);
            shopping1.setFnum(fnum);
            shopping1.setFid(flower.getFid());
            shopping1.setUid(uid);
            shopping1.setPrice(flower.getPrice());
            int result = shoppingService.addShopping(shopping1);
            if (result != -1) {
                map.put("code", 200);
                map.put("msg", "添加成功");
            } else {
                map.put("code", 201);
                map.put("msg", "添加失败");
            }
        } else {
            Shopping shopping2 = new Shopping();
            shopping2.setFid(flower.getFid());
            shopping2.setFnum(fnum);
            int result = shoppingService.updateFnum(shopping2);
            if (result != -1) {
                map.put("code", 200);
                map.put("msg", "添加成功");
            } else {
                map.put("code", 201);
                map.put("msg", "添加失败");
            }
        }
        return map;
    }
}
